package two.chapter_1.c1_2;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;

public class Interval2D {
    private Interval1D x,y;
    public Interval2D(Interval1D x,Interval1D y){
        this.x=x;
        this.y=y;
    }
    public double area(){
        return x.length()*y.length();
    }
    public boolean contains(Point2D p){

        return x.contains(p.x())  && y.contains(p.y());
    }
    public void draw(){
        double xc = (x.getLo() + x.getHi()) / 2.0;
        double yc = (y.getLo() + y.getHi()) / 2.0;
        StdDraw.rectangle(xc, yc, x.length() / 2.0, y.length() / 2.0);
    }

    public static void main(String []args){
        double xlo=.2;
        double xhi=.5;
        double ylo=.5;
        double yhi=.6;
        int T=10000;

        Interval1D x=new Interval1D(xlo,xhi);
        Interval1D y=new Interval1D(ylo,yhi);
        Interval2D box=new Interval2D(x,y);
        box.draw();
        Counter hits=new Counter("hits");
        for (int i=0;i<T;i++){
            double a=Math.random();
            double b=Math.random();
            Point2D p=new Point2D(a,b);
            if (box.contains(p)){
                hits.increment();
            }else {
                p.draw();
            }

        }

        StdOut.println(hits);
        StdOut.println(box.area());
    }
}
